import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list=new ArrayList<>();
        int len=words[0].length();
        int left=0;
        int right=0;
        int count=0;
        int m=words.length;


        HashMap<String,Integer> hash=new HashMap<>();
        for(String s1:words) hash.put(s1,hash.getOrDefault(s1,0)+1);

        for(int i=0;i<len;i++){

            left=right=i;
            count=0;
            

            HashMap<String,Integer> hash2=new HashMap<>();

            while(right<=s.length()-len){

                String in=s.substring(right,right+len);
                hash2.put(in,hash2.getOrDefault(in,0)+1);
                if(hash2.get(in)<=hash.getOrDefault(in,0)){
                    count++;
                }

                if(right-left+1>m*len){
                    String out=s.substring(left,left+len);
                    if(hash2.get(out)<=hash.getOrDefault(out,0)){
                        count--;
                    }
                    hash2.put(out,hash2.get(out)-1);

                    left+=len;
                }

                if(count==m) list.add(left);
                right+=len;

            }
        }
        return list;

    }
}
